import util from '../tool/util'
import { UI_Layer, UI_Listen, RedDot_Key } from '../../config/ui_def'
import { WX_AD_CONFIG, Sdk_Platform, VIVO_AD_CONFIG, OPPO_AD_CONFIG, Ad_Type } from '../../frame/constants';
cc.Class({
    extends: cc.Component,

    properties: {
        content: cc.Node,
        closeBtn: cc.Node,
        adBanner: cc.Node,
        adIcon: cc.Node,
        sign_node: cc.Node,
        haveSign_node: cc.Node,
    },

    // update (dt) {},
    initPage() {

    },
    updatePage() {
        this.adSwitch(true)
        switch (common.sdk_env) {
            case Sdk_Platform.VIVO:
                this.closeBtn.setPosition(333, 395)
                for (let a = 0; a < this.content.children.length; a++) {
                    this.content.children[a].getComponent(cc.Button).interactable = false
                }
                this.adIcon.active = true
                break
            default:
                this.closeBtn.setPosition(0, -530)
                this.adIcon.active = false
                for (let b = 0; b < this.content.children.length; b++) {
                    this.content.children[b].getComponent(cc.Button).interactable = true
                }
                break
        }
        let rewardList = he.db.getTable('Reward')
        this.rewards = []
        if (rewardList && rewardList.length > 0) {
            for (let i = 0; i < rewardList.length; i++) {
                if (rewardList[i].type == 10001) {
                    this.rewards.push(rewardList[i])
                }
            }
        }
        this.setPic()
        this.sign_histroy = he.local.getJson('sign_histroy', 'user')
        let dida = he.local.get('local_dida')
        if (!dida) {
            this.dida = 0
        } else {
            this.dida = dida
        }
        if (!this.sign_histroy) {
            this.sign_histroy = []
        }
        if (this.sign_histroy.length >= 7 && util.checkCrossDay(this.sign_histroy[this.sign_histroy.length - 1])) {
            this.sign_histroy = []
            he.local.setJson('sign_histroy', this.sign_histroy, 'user')
        }
        if (this.sign_histroy.length && !util.checkCrossDay(this.sign_histroy[this.sign_histroy.length - 1])) {
            this.haveSign_node.active = true
            this.sign_node.active = false
        } else {
            this.haveSign_node.active = false
            this.sign_node.active = true
        }
        this.isShowBanner = false
        this.updateRewards()
    },
    closeUI() {
        this.adSwitch(false)
        if (this.sc1) {
            this.unschedule(this.bannerInit)
            this.sc1 = null
        }

        if (this.sc2) {
            this.unschedule(this.hideBanner)
            this.sc2 = null
        }
        gg.uiManager.closePage(UI_Layer.Msg, 'chayeAdFake')
        gg.eventManager.emit('main', { type: UI_Listen.MAIN_NEWPAGE, pageName: 'BottomBar' })

    },

    adSwitch(status) {
        if (status) {
            switch (common.sdk_env) {
                case Sdk_Platform.OPPO:
                    let guideList = he.local.getJson('guideList', 'user')
                    if (!guideList[6]) {
                        return
                    }
                    gg.uiManager.addPage(UI_Layer.Msg, {
                        name: 'chayeAdFakeOPPO',
                        isShowAd: true,
                        isShowBanner: false,
                        adIndex: 6,
                        position: cc.Vec2(0, -650)
                    })
                    break
                case Sdk_Platform.HUAWEI:
                    gg.uiManager.addPage(UI_Layer.Msg, {
                        name: 'hwAdPage',
                        cover: false,
                        isShowAd: true,
                        adType: Ad_Type.INSERT,
                        position: 0
                    })
                    break
            }
        }
        else {
            switch (common.sdk_env) {
                case Sdk_Platform.OPPO:
                    gg.uiManager.closePage(UI_Layer.Msg, 'chayeAdFakeOPPO')
                    break
                case Sdk_Platform.HUAWEI:
                    if (gg.uiManager.getPage(UI_Layer.Msg, 'hwAdPage')) {
                        gg.uiManager.closePage(UI_Layer.Msg, 'hwAdPage')
                    }
                    break
            }
        }
    },

    colseServer() {
        switch (common.sdk_env) {
            case Sdk_Platform.WECHAT:
                let ver_info = he.local.getJson('ver_info')
                if (ver_info.banner_mistake) {
                    if (!this.isShowBanner) {
                        this.isShowBanner = true
                        this.sc1 = this.scheduleOnce(this.bannerInit, 0.5)
                        this.sc2 = this.scheduleOnce(this.hideBanner, 2)
                    } else {
                        this.uiBase.closePage()
                    }
                } else {
                    this.uiBase.closePage()
                }
                break
            default:
                this.uiBase.closePage()
                break
        }
    },

    bannerInit() {
        this.adBanner.name = WX_AD_CONFIG.bannerId[0]
        /// 显示
        he.ad.changeBanner(true, this.adBanner)
    },

    hideBanner() {
        this.adBanner.name = WX_AD_CONFIG.bannerId[0]
        he.ad.changeBanner(false, this.adBanner)
    },

    setPic() {
        for (let i = 0; i < this.content.children.length; i++) {
            let sp = this.content.children[i].getChildByName('icon').getComponent(cc.Sprite)
            if (i != 1) {
                let name = `day_${i + 1}`
                sp.spriteFrame = he.load.get('signPage', cc.SpriteAtlas, 'plist').getSpriteFrame(name)
            } else {
                if (this.checkTeam1003()) {
                    let name = `day2_2`
                    sp.spriteFrame = he.load.get('signPage', cc.SpriteAtlas, 'plist').getSpriteFrame(name)
                } else {
                    let name = `day_2_1`
                    sp.spriteFrame = he.load.get('signPage', cc.SpriteAtlas, 'plist').getSpriteFrame(name)
                }
            }
        }
    },


    //reward要读表
    updateRewards() {
        for (let i = 0; i < this.rewards.length; i++) {
            let reward_node = this.content.getChildByName("day" + (i + 1))
            let info = util.copy(this.rewards[i])
            if (i < this.sign_histroy.length) {
                info.style = 2 // 签了
            } else if ((i == this.sign_histroy.length && i == 0) || (i == this.sign_histroy.length && this.sign_histroy.length && util.checkCrossDay(this.sign_histroy[this.sign_histroy.length - 1]))) {
                info.style = 1 // 今天
            } else {
                info.style = 3 // 没签
            }
            this.setReward(reward_node, info)
        }
    },
    setReward(node, param) {
        let light_node = node.getChildByName("light")
        let get_node = node.getChildByName("haveGet")
        let icon_node = node.getChildByName("icon")
        switch (param.style) {

            case 1:
                light_node.active = true
                get_node.active = false
                this.light_tween = cc.tween(light_node)
                    .by(5, { angle: 360 })
                    .repeatForever()
                    .start()
                break
            case 2:
                light_node.active = false
                get_node.active = true
                break
            case 3:
                light_node.active = false
                get_node.active = false
                break
        }
    },

    signFunc() {
        let info = this.rewards[this.sign_histroy.length]
        let reward = null
        reward = info.reward
        // console.log(reward)
        let rewardGrp
        if (reward.indexOf("/") != -1) {
            let newgrp = reward.split('/')
            if (this.checkTeam1003()) {
                rewardGrp = newgrp[1].split('#')
            } else {
                rewardGrp = newgrp[0].split('#')
            }
        } else {
            rewardGrp = reward.split('#')
        }

        for (let i = 0; i < rewardGrp.length; i++) {
            let itemInfo = rewardGrp[i].split('_')
            if (itemInfo[0] && itemInfo[1]) {
                gg.data.addItem(parseInt(itemInfo[0]), parseInt(itemInfo[1]))
            }
        }
        // let infoGrp = info.split('_')
        //领取奖励
        gg.eventManager.emit('topBar', UI_Listen.TOP_UPDATE_MONEY)
        let index = this.sign_histroy.length + 1
        let reward_node = this.content.getChildByName("day" + index)
        info.style = 2
        this.setReward(reward_node, info)
        let now = new Date().getTime() + this.dida
        this.sign_histroy.push(now)
        this.haveSign_node.active = true
        this.sign_node.active = false
        he.local.setJson('sign_histroy', this.sign_histroy, 'user')
        gg.redManager.updateRedDot(RedDot_Key.DAY_SIGN)
    },
    checkTeam1003() {
        let team = he.local.getJson('team', 'user')
        if (team && team.length > 0) {
            let isHave = false
            for (let i = 0; i < team.length; i++) {
                if (team[i].id == 1003) {
                    isHave = true
                    return true
                }
            }
            if (!isHave) {
                return false
            }
        }
    },

    adAdd() {
        let taskGrp = he.local.getJson('task', 'user')
        if (taskGrp && taskGrp.length > 0) {
            for (let j = 0; j < taskGrp.length; j++) {
                if (taskGrp[j].type == 9001) {
                    if (taskGrp[j].num < taskGrp[j].need) {
                        taskGrp[j].num += 1
                    }
                    break
                }
            }
        }
        he.local.setJson('task', taskGrp, 'user')
    },

    mulFunc() {
        //广告点记录
        switch (common.sdk_env) {
            case Sdk_Platform.WECHAT:
                break
            case Sdk_Platform.VIVO:
                he.local.click(`vivo_video_signPage`)
                break

        }

        let info = this.rewards[this.sign_histroy.length]
        let reward = null
        reward = info.ad_reward
        let rewardGrp = reward.split('#')
        for (let i = 0; i < rewardGrp.length; i++) {
            let itemInfo = rewardGrp[i].split('_')
            if (itemInfo[0] && itemInfo[1]) {
                gg.data.addItem(parseInt(itemInfo[0]), parseInt(itemInfo[1]))
            }
        }
        // let infoGrp = info.split('_')
        //领取奖励
        gg.eventManager.emit('topBar', UI_Listen.TOP_UPDATE_MONEY)
        let index = this.sign_histroy.length + 1
        let reward_node = this.content.getChildByName("day" + index)
        info.style = 2
        this.setReward(reward_node, info)
        let now = new Date().getTime() + this.dida
        this.sign_histroy.push(now)
        he.local.setJson('sign_histroy', this.sign_histroy, 'user')
        this.adAdd()
        this.haveSign_node.active = true
        this.sign_node.active = false
        gg.eventManager.emit('rewardTip')
        gg.redManager.updateRedDot(RedDot_Key.DAY_SIGN)
    },



    clickSign() {
        if (this.sign_histroy.length >= 7) {
            gg.uiManager.addTip("等待明天更新签到内容")
            return
        }
        let info
        if (this.sign_histroy.length && !util.checkCrossDay(this.sign_histroy[this.sign_histroy.length - 1])) {
            gg.uiManager.addTip("今天已经签到了")
        } else {

            info = this.rewards[this.sign_histroy.length]
            let reward = null
            reward = info.reward
            let rewardGrp
            if (reward.indexOf("/") != -1) {
                let newgrp = reward.split('/')
                if (this.checkTeam1003()) {
                    rewardGrp = newgrp[1].split('#')
                } else {
                    rewardGrp = newgrp[0].split('#')
                }
            } else {
                rewardGrp = reward.split('#')
            }
            switch (common.sdk_env) {
                case Sdk_Platform.VIVO:
                    he.ad.showVideo(this.vivoShowAd.bind(this))
                    break
                default:
                    gg.uiManager.addPage(UI_Layer.Cover, {
                        name: 'rewardPage',
                        itemGrp: rewardGrp,
                        get_func: this.signFunc.bind(this),
                        ad_func: (res => {
                            switch (common.sdk_env) {
                                case Sdk_Platform.WECHAT:
                                    he.ad.showVideo(this.mulFunc.bind(this))
                                    break
                                case Sdk_Platform.VIVO:
                                    he.ad.showVideo(this.mulFunc.bind(this))
                                    break
                                case Sdk_Platform.OPPO:
                                    he.ad.showVideo(OPPO_AD_CONFIG.videoId[6], this.mulFunc.bind(this))
                                    break
                                case Sdk_Platform.UC:
                                    he.ad.showVideo(this.mulFunc.bind(this))
                                    break
                                case Sdk_Platform.HUAWEI:
                                    he.ad.showVideo(this.mulFunc.bind(this))
                                    break
                                default:
                                    this.mulFunc()
                                    break
                            }
                        }),
                        cover: true,
                        close: true,
                    })
                    break
            }



            // this.signFunc()
        }
    },

    vivoShowAd() {
        let info = this.rewards[this.sign_histroy.length]
        let reward = null
        reward = info.reward
        let rewardGrp
        if (reward.indexOf("/") != -1) {
            let newgrp = reward.split('/')
            if (this.checkTeam1003()) {
                rewardGrp = newgrp[1].split('#')
            } else {
                rewardGrp = newgrp[0].split('#')
            }
        } else {
            rewardGrp = reward.split('#')
        }
        // this.signFunc()
        gg.uiManager.addPage(UI_Layer.Cover, {
            name: 'rewardPage',
            itemGrp: rewardGrp,
            get_func: this.signFunc.bind(this),
            ad_func: (res => {
                switch (common.sdk_env) {
                    case Sdk_Platform.WECHAT:
                        he.ad.showVideo(this.mulFunc.bind(this))
                        break
                    case Sdk_Platform.VIVO:
                        he.ad.showVideo(this.mulFunc.bind(this))
                        break
                    case Sdk_Platform.OPPO:
                        he.ad.showVideo(OPPO_AD_CONFIG.videoId[6], this.mulFunc.bind(this))
                        break
                    case Sdk_Platform.UC:
                        he.ad.showVideo(this.mulFunc.bind(this))
                        break
                    case Sdk_Platform.HUAWEI:
                        he.ad.showVideo(this.mulFunc.bind(this))
                        break
                    default:
                        this.mulFunc()
                        break
                }
            }),
            cover: true,
            close: true,
        })
    },

    haveSignServer() {
        gg.uiManager.addTip("今天已经签到了")
    },


    sevenBtnServer(code, param) {
        let info = this.rewards[param - 1]
        if (parseInt(param) - 1 < this.sign_histroy.length) {
            return
        } else {
            let reward = null
            reward = info.reward
            let rewardGrp
            if (reward.indexOf("/") != -1) {
                let newgrp = reward.split('/')
                if (this.checkTeam1003()) {
                    rewardGrp = newgrp[1].split('#')
                } else {
                    rewardGrp = newgrp[0].split('#')
                }
            } else {
                rewardGrp = reward.split('#')
            }
            if ((parseInt(param) - 1) == this.sign_histroy.length) {
                if (this.sign_histroy.length && !util.checkCrossDay(this.sign_histroy[this.sign_histroy.length - 1])) {
                    gg.uiManager.addPage(UI_Layer.Cover, {
                        name: 'rewardPage',
                        itemGrp: rewardGrp,
                        cover: true,
                        close: true,
                    })
                } else {
                    gg.uiManager.addPage(UI_Layer.Cover, {
                        name: 'rewardPage',
                        itemGrp: rewardGrp,
                        get_func: this.signFunc.bind(this),
                        ad_func: (res => {
                            switch (common.sdk_env) {
                                case Sdk_Platform.WECHAT:
                                    he.ad.showVideo(this.mulFunc.bind(this))
                                    break
                                case Sdk_Platform.VIVO:
                                    he.ad.showVideo(this.mulFunc.bind(this))
                                    break
                                case Sdk_Platform.OPPO:
                                    he.ad.showVideo(OPPO_AD_CONFIG.videoId[6], this.mulFunc.bind(this))
                                    break
                                case Sdk_Platform.UC:
                                    he.ad.showVideo(this.mulFunc.bind(this))
                                    break
                                case Sdk_Platform.HUAWEI:
                                    he.ad.showVideo(this.mulFunc.bind(this))
                                    break
                                default:
                                    this.mulFunc()
                                    break
                            }
                        }),
                        cover: true,
                        close: true,
                    })
                }
            } else {
                gg.uiManager.addPage(UI_Layer.Cover, {
                    name: 'rewardPage',
                    itemGrp: rewardGrp,
                    cover: true,
                    close: true,
                })
            }

        }
    },


});
